<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>品牌管理</title>
<!--    <meta http-equiv="X-UA-Compatible" content="IE=edge">-->
<!--    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">-->
<!--    <script src="../js/vue.js"></script>-->
<!--    <script src="https://unpkg.com/element-ui/lib/index.js"></script>-->
<!--    <script src="../js/axios.js"></script>-->
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">

    <style>
        .avatar-uploader .el-upload {
            border: 1px dashed #d9d9d9;
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
        }

        .avatar-uploader .el-upload:hover {
            border-color: #409EFF;
        }

        .avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 178px;
            height: 178px;
            line-height: 178px;
            text-align: center;
        }

        .avatar {
            width: 178px;
            height: 178px;
            display: block;
        }
    </style>
</head>
<body>
<div id="app" >
    <div class="content-header">
        <h1>管理<small>品牌管理</small></h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>管理</el-breadcrumb-item>
            <el-breadcrumb-item>brand管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>

    <el-form :inline="true" class="demo-form-inline">
        <el-form-item label="品牌名称">
            <el-input placeholder="品牌名称" v-model="searchMap.name"></el-input>
        </el-form-item>

        <el-form-item label="首字母">
            <el-input placeholder="首字母" v-model="searchMap.letter"></el-input>
        </el-form-item>

        <el-form-item>
            <el-button type="primary" @click="handleCurrentChange">查询</el-button>
        </el-form-item>

        <el-form-item>
            <el-button type="primary" @click="pojo={}, imageUrl='',centerDialogVisible = true">新增</el-button>
        </el-form-item>
    </el-form>


    <el-dialog
            title="编辑"
            :visible.sync="centerDialogVisible">


        <el-form>
            <el-form-item label="品牌名称">
                <el-input v-model="pojo.name"></el-input>
            </el-form-item>

            <el-form-item label="首字母">
                <el-input v-model="pojo.letter"></el-input>
            </el-form-item>

            <el-form-item label="排序">
                <el-input v-model="pojo.seq"></el-input>
            </el-form-item>

            <el-form-item label="图片">
                <el-upload
                        class="avatar-uploader"
                        action="/upload/native.do"
                        :show-file-list="false"
                        :on-success="handleAvatarSuccess"
                        :before-upload="beforeAvatarUpload">
                    <img v-if="imageUrl" :src="imageUrl" class="avatar">
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
            </el-form-item>


            <el-form-item>
                <el-button @click="save" type="primary">提交</el-button>
                <el-button @click="centerDialogVisible = false" type="primary">关闭</el-button>
            </el-form-item>
        </el-form>


    </el-dialog>


    <el-table
            :data="tableData"
            style="width: 100%">
        <el-table-column
                prop="id"
                label="id"
                width="180">
        </el-table-column>
        <el-table-column
                prop="name"
                label="品牌名称"
                width="180">
        </el-table-column>
        <el-table-column
                prop="letter"
                label="首字母"
                width="180">
        </el-table-column>
        <el-table-column
                prop="seq"
                label="排序">
        </el-table-column>
        <el-table-column
                prop="image"
                label="图片地址">
            <template slot-scope="scope">
                <el-image
                        style="width: 100px; height: 100px"
                        :src="scope.row.image"
                        fit="scale-down">

                </el-image>
                <!--                <img :src="scope.row.image"/>-->
            </template>
        </el-table-column>
        <el-table-column
                label="操作">
            <template slot-scope="scope">
                <el-button @click="edit(scope.row.id) "><i class="el-icon-edit"></i></el-button>
                <el-button @click="dele(scope.row.id)"><i class="el-icon-delete"></i></el-button>
            </template>
        </el-table-column>


    </el-table>

    <el-pagination
            @size-change="handleCurrentChange"
            @current-change="handleCurrentChange"
            :current-page.sync="currentPage"
            :page-sizes="[10, 20, 30, 40]"
            :page-size.sync="size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
    </el-pagination>
</div>
</body>
</html>
<script src="/js/vue.js"></script>
<script src="/js/axios.js"></script>
<script src="/js/elementui.js"></script>
<script>
    new Vue({
        el: "#app",
        data() {
            return {
                tableData: [],
                currentPage: 1,
                size: 10,
                total: 0,
                searchMap: {},
                centerDialogVisible: false,
                pojo: {},
                imageUrl: ''
            }
        },
        created() {
            debugger;
            this.handleCurrentChange();
        },
        methods: {
            handleCurrentChange() {
                debugger;
                let _this = this;
                axios.post(`/brand/findPage.do?page=${this.currentPage}&size=${this.size}`, this.searchMap).then(res => {

                    _this.tableData = res.data.rows;
                    _this.total = res.data.total;

                });
            },
            save() {
                this.pojo.image = this.imageUrl;
                let _this = this;
                axios.post(`/brand/${this.pojo.id == null ? 'add' : 'update'}.do`, this.pojo).then(res => {
                    _this.centerDialogVisible = false;
                    _this.handleCurrentChange();
                })
            },
            edit(id) {
                this.centerDialogVisible = true;
                let _this = this;
                axios.get(`/brand/findById.do?id=${id}`).then(res => {
                    console.log(res.data);
                    _this.pojo = res.data;
                    _this.imageUrl = _this.pojo.imag;
                })
            },
            dele(id) {
                this.$confirm('要删除吗?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    axios.get(`/brand/delete.do?id=${id}`).then(res => {
                        if (res.data.code == 0) {
                            this.handleCurrentChange();
                            this.$message({
                                type: 'success',
                                message: '删除成功!'
                            });
                        } else {
                            this.$message({
                                type: 'error',
                                message: '删除失败!'
                            });
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },

            handleAvatarSuccess(res, file) {
                this.imageUrl = file.response;
            },
            beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg';
                const isLt2M = file.size / 1024 / 1024 < 2;

                if (!isJPG) {
                    this.$message.error('上传头像图片只能是 JPG 格式!');
                }
                if (!isLt2M) {
                    this.$message.error('上传头像图片大小不能超过 2MB!');
                }
                return isJPG && isLt2M;
            }
        }
    })


</script>